﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>文件管理</title>
    $css()
    <!--<link rel="Stylesheet" href="admin?res=c3R5bGU=&amp;.css" />-->
    <style type="text/css">/*
        *{font-size:12px;}
         td{background:white;padding:5px 5px;margin-top:-2px;border-bottom:dotted 1px #e0e0e0;border-right:dotted 1px #e0e0e0;}
         th{background:#f0f0f0;line-height:25px;border-right:solid 1px #e5e5e5;border-bottom:solid 1px #e5e5e5}*/
         .center{text-align:center;}
         .hidden{display:none}
        .dir-name{color:#006680;font-weight:bold;padding-left:5px;}
        .file-name{padding-left:5px;}
        .th-split{display:none;}
    </style>
</head>
<body>


 <div id="control" style="position:absolute;left:253px;top:30px;">
    <span class="left"><strong>新建文件:</strong><input type="text" name="filename" id="create_filename" />&nbsp;</span>
    <a class="btn" onclick="ctobj.newfile('create_filename')" href="javascript:;">创建</a>
</div>

<table style="width:100%" cellspacing="0" cellpadding="0" id="filesexp" class="ui-table">
 <thead>
 <tr>
    <th class="hidden"></th>
    <th style="width:250px;text-align:left;padding-left:5px;">文件名</th>
    <th>大小</th>
    <th>类型</th>
    <th>创建时间</th>
    <th>修改时间</th>
    <th>只读</th>
    <th style="border-right:none">下载/编辑/更名</th>
 </tr></thead>
 <tbody>
 </tbody>
</table>


$js()
<script type="text/javascript">
    /*var cms = new js();*/
    var fm_handler = '?module=file&action=filemanager';
    var curdir = (location.hash || '#/').substring(1);
    var tb = cms.$('filesexp');

    var ctobj = {
        tip: function (msg, error) {
            window.parent.M.msgtip({ html: msg, autoClose: true });
        },

        resetTable: function () {
            var leng = tb.rows.length;
            for (var i = 1; i < leng; i++) {
                tb.deleteRow(1);
            }

            var parentDir = '/';
            if (location.hash) {
                parentDir = location.hash.replace(/[^\/]+\/\B/, '').replace('#', '');
            }

            var tr = tb.insertRow(-1);  //插入倒数第一行
            var cell = tr.insertCell(-1);
            cell.setAttribute('colspan', '8');
            cell.innerHTML = '<a style="color:#0066cc" href="#' + encodeURIComponent(parentDir) + '">[⇮上一级]</a>';
        },
        getFileLen: function (_len) {
            var len = _len;
            if (len > 1024 * 1024) {
                len = (len / 1024 / 1024);
                if (/\.\d\d/.test(len.toString())) {
                    len = len.toFixed(2);
                }
                len = len + 'M';
            } else if (len > 1024) {
                len = (len / 1024);
                if (/\.\d\d/.test(len.toString())) {
                    len = len.toFixed(2);
                }
                len = len + 'K';
            } else {
                len = len + 'b';
            }
            return len;
        },
        getFileType: function (name) {
            switch (name) {
                case 'Web.config': return 'ASP.NET网站配置文件';
                case 'Global.asax': return '全局应用程序文件';
                case 'web.conf': return 'CMS配置文件';
                case 'plugin.config':return '插件配置文件';
            }
            var ext = name.replace(/[\s\S]+\.(.+)\b/, '$1');
            switch (ext.toLowerCase()) {
                case "conf":return '设置文件';
                case "jpeg":
                case 'gif':
                case 'png':
                case 'bmp':
                case 'jpg':
                    return ext + '图像格式';

                case 'dll': return '二进制代码文件';
                case 'html': return 'HTML文件';
                case 'phtml': return '部分视图文件';
            }
            return ext.toUpperCase() + '文件';
        },
        insertCell: function (row, html, className, col) {
            var td = row.insertCell(-1);
            td.innerHTML = html;
            if (className) {
                td.className = className;
            }
            if (col) {
                td.setAttribute('colspan', col);
            }
            return td;
        },
        appendDirRow: function (o) {
            var row = tb.insertRow(-1);
            this.insertCell(row, '<input type="hidden" file="' + o.name + '"/>', 'hidden');
            this.insertCell(row, '<a href="#' + curdir + encodeURIComponent(o.name) + '/">' + o.name + '</a>', 'dir-name');
            this.insertCell(row, (o.dirnum == 0 ? '' : '目录:' + o.dirnum + '个&nbsp;') +
            (o.filenum == 0 ? '' : '文件:' + o.filenum + '个'), null, 5);

            this.insertCell(row, (o.system == 1 ? '' :
            '<a href="javascript:;" onclick="ctobj.rename(this,\'dir\')">重命名</a>' +
            '|<a href="javascript:;" onclick="ctobj.del(this,\'dir\')">删除</a>'
            ), 'center');
        },
        appendFileRow: function (o) {
            var row = tb.insertRow(-1);
            this.insertCell(row, '<input type="checkbox" file="' + o.name + '"/>', 'hidden');
            this.insertCell(row, o.name, 'file-name');
            this.insertCell(row, this.getFileLen(o.len));
            this.insertCell(row, this.getFileType(o.name));
            this.insertCell(row, o.date, 'center');
            this.insertCell(row, o.mdate, 'center');
            this.insertCell(row, o.readonly == 1 ? '只读' : '可写', 'center');
            this.insertCell(row, '<a href="javascript:;" onclick="ctobj.down(this)">下载</a>' +
                                    (o.system == 1 || o.readonly==1 ? '' :
                                    '|<a href="javascript:;" onclick="ctobj.edit(this)">编辑</a>' +
                                    '|<a href="javascript:;" onclick="ctobj.rename(this,\'file\')">重命名</a>' +
                                    '|<a href="javascript:;" onclick="ctobj.del(this,\'file\')">删除</a>'
                                    ), 'center');
        },
        del: function (t, type) {
            var _this = this;
            var tr = t.parentNode.parentNode;
            var trIndex = 0;

            for (var i = 0; i < tb.rows.length; i++) {
                if (tb.rows[i] == tr) {
                    trIndex = i;
                }
            }

            var file = tr.getElementsByTagName('TD')[0].childNodes[0].getAttribute('file');

            if (confirm('删除后无法恢复,确定要删除 "' + file + '" 吗,?')) {
                var isDir = type == 'dir' ? 1 : 0;
                cms.xhr.post(fm_handler, 'act=del&dir='
                    + encodeURIComponent(curdir) + '&isdir=' + isDir + '&file='
                     + encodeURIComponent(file), function (result) {

                         if (result == "1") {
                             //load(curdir); /*重新加载*/
                             tb.deleteRow(trIndex);
                         } else {
                             _this.tip('系统文件/目录无法删除!', true);
                         }

                     }, function () {
                         _this.tip('删除失败' + (isDir == 1 ? ',目录不为空或' : '') + '无权限删除!', true);
                     });
            }
        },
        rename: function (t, type) {
            var _this = this;
            var tr = t.parentNode.parentNode;
            var tds = tr.getElementsByTagName('TD');
            var file = tds[0].childNodes[0].getAttribute('file');
            var isDir = type == 'dir' ? 1 : 0;
            if (tds[1].getElementsByTagName('INPUT').length == 0) {
                var iput = document.createElement('INPUT');
                iput.value = file;
                tds[1].innerHTML = '';
                tds[1].appendChild(iput);
                t.innerHTML = '保存';
            } else {
                var newfile = tds[1].childNodes[0].value;
                if (file == newfile) {
                    var a = document.createElement("A");
                    a.href = "#" + curdir + encodeURIComponent(file) + "/";
                    a.innerHTML = file;
                    tds[1].innerHTML = '';
                    tds[1].appendChild(a);
                } else {
                    _this.tip('执行更改...');
                    cms.xhr.post(fm_handler, 'act=rename&isdir=' + isDir + '&dir='
                    + encodeURIComponent(curdir) + '&file=' + encodeURIComponent(file)
                    + '&newfile=' + encodeURIComponent(newfile),
                 function (result) {
                     /*_this.tip('');*/
                     load(curdir);
                 }, function (x) {
                     _this.tip('操作失败!');
                 }
                    );
                }

                t.innerHTML = '重命名';
            }
        },
        edit: function (t) {
            var tr = t.parentNode.parentNode.getElementsByTagName('TD')[0];
            var file = tr.childNodes[0].getAttribute('file');
            var ext = file.replace(/[\s\S]+\.(.+)\b/, '$1');
            var editurl = null;
            switch (ext.toLowerCase()) {
                case "css":
                case "js":
                case "txt":
                case "conf":
                case 'config':
                case 'log':
                case "aspx":
                case "xml":
                case "cs":
                    editurl = '?module=file&action=editfile&path=';
                    break;
                case "html":
                case "phtml":
                    editurl = '?module=template&action=editfile&path=';
                    break;
            }
            if (editurl != null) {
               // window.open(editurl + curdir + file);
               parent.tab.show(file,editurl + curdir + file);
            } else {
                this.tip('不支持编辑的文件类型!', true);
            }
        },
        newfile: function (id) {
            var _this = this;
            var fileName = cms.val(id);
            cms.xhr.post(fm_handler, 'act=create&dir=' + encodeURIComponent(curdir) + '&file=' + encodeURIComponent(fileName),
            function (result) {
                var json = cms.toJson(result);
                if (json.error) {
                    _this.tip(json.error, true);
                } else {
                    load(curdir);
                }
            }, function (x) {
            });
        },
        down: function (t) {
            var tr = t.parentNode.parentNode.getElementsByTagName('TD')[0];
            var file = tr.childNodes[0].getAttribute('file');
            window.open('http://' + document.domain + curdir + file);
        }
    };

    function load(dir) {
       // ctobj.tip('加载中...', 0);
        ctobj.resetTable();
        cms.xhr.post(fm_handler, 'act=list&dir=' + dir, function (result) {
            var json = cms.toJson(result);
            if (json.error) {
                ctobj.tip(json.error, true);
            } else {
                //文件夹
                for (var i = 0; i < json.dirs.length; i++) {
                    ctobj.appendDirRow(json.dirs[i]);
                }

                //文件
                for (var i = 0; i < json.files.length; i++) {
                    ctobj.appendFileRow(json.files[i]);
                }


                //重新设置风格
                cms.table.dynamic(document.getElementsByTagName("TABLE")[0],false,function (row) {
                });

            }
        }, function (x) { });

    }

    var _hash = curdir;
    setInterval(function () {
        _hash = (location.hash || '#/').substring(1);
        if (curdir != _hash) {
            curdir = _hash;
            load(curdir);
        }
    }, 100);

   load(curdir);
</script>



</body>
</html>
